/**
 * 
 */
package com.ra.landlord;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.googlecode.cswish.annotation.Rest;
import com.googlecode.cswish.model.PageInfo;
import com.googlecode.cswish.oosearch.QLInfo;
import com.googlecode.cswish.struts.spring.GenericService;
import com.googlecode.cswish.struts.spring.SearchCallback;

/**
 * 工作日志服务
 * 
 * @author colin
 * @date 2015年3月21日
 */
@Service
public class WorkLogService {

	@Resource
	private GenericService genericService;

	@Resource
	private LandlordUserService landlordUserService;

	/**
	 * 查询工作日志列表
	 * 
	 * @return
	 */
	@Rest(type="search")
	public List<WorkLog> searchWorkLog(WorkLog model) {
		PageInfo<WorkLog> pageInfo=genericService.search(model, 0, 1000, "type", "asc", false);
		return pageInfo.getData();
	}
	
	private void addCondition(StringBuilder qlBuilder, String logicFlag, String condition) {
		if (qlBuilder.indexOf("where")  == -1) {
			qlBuilder.append(" "+"where");
			qlBuilder.append(" "+condition);
		} else {
			qlBuilder.append(logicFlag +" " + condition);
		}
		
	}
	
	@SuppressWarnings("unchecked")
	@Rest(type = "search")
	public PageInfo<WorkLog> search(WorkLog model, Integer processType,int pageIndex,int pageSize) throws ParseException {
		List<Object> params = new ArrayList<Object>();
		PageInfo<WorkLog> pageInfo = null;
		if (pageSize == 0) {
			pageSize = 15;
			pageIndex = 1;
		}
	
		StringBuilder qlBuilder = new StringBuilder("select a from WorkLog a");
		if (model.getType() != null) {
			addCondition(qlBuilder, "and", "a.type = ?");
			params.add(model.getType());
		}
		//processType 0已处理 1未处理 2过期未处理
		//handleType 0 - 不处理， 1 - 处理中， 2 - 已处理， 3 - 未处理
		if (processType != null) {
			if (processType == 0) {
				addCondition(qlBuilder, "and", "a.handleType = ?");
				params.add(2);
			} else if (processType == 1){
				addCondition(qlBuilder, "and", "a.handleType = ?");
				params.add(3);
			} else {
				addCondition(qlBuilder, "and", "a.handleType = ?");
				params.add(3);
				
				Calendar now = Calendar.getInstance();
				addCondition(qlBuilder, "and", "a.remindTime < ?");
				SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
				params.add(sdf.parse(sdf.format(now.getTime())));
				
			}
		}
		QLInfo qlInfo = new QLInfo(qlBuilder.toString(), false, params);
		qlInfo.setFirstResult((pageIndex - 1) * pageSize);
		qlInfo.setMaxResults(pageSize);
		pageInfo = genericService.searchByQl(qlInfo);
	
		pageInfo.setPageSize(pageSize);
		pageInfo.setTotalCount( genericService.getCount(qlInfo, false));
		return pageInfo;
	}
	@SuppressWarnings("unchecked")
	@Rest(simpleServiceName="loginUser",type="search")
	public List<WorkLog> searchWorkLogByRemindTime(String remindTime) {
		QLInfo ql=new QLInfo("select a from WorkLog a where a.remindTime='"+remindTime+"'", false);
		return genericService.searchByQl(ql).getData();
	}
	/**
	 * 新增工作日志
	 * @param model
	 * @return
	 */
	@Rest(type="update")
	public WorkLog addWorkLog(WorkLog model) {
		LandlordUser landlordUser = landlordUserService.getLoginUser();
		model.setAgent(landlordUser);
		model.setCreateTime(new Date());
		model.setRemind(false);
		if (model.getType() <= 6) {
			model.setExType(0);
		} else {
			model.setExType(1);
		}
		return genericService.add(model);
	}

	/**
	 * 修改工作日志
	 * 
	 * @param model
	 * @return
	 */
	@Rest(type="update")
	public WorkLog updateWorkLog(WorkLog model) {
		Collection<String> enableFields = Arrays.asList("type", "remind",
				"remindTime", "handleType", "title", "content");
		return genericService.update(model, false, enableFields, null);
	}

	/**
	 * 删除工作日志
	 * 
	 * @param model
	 * @return
	 */
	@Rest(type="update")
	public boolean deleteWorkLog(List<Object> models) {
		return genericService.deleteList(models);
	}

	@Rest(simpleServiceName="loginUser",type="free")
	public Set<Integer> getDayInMonthOfWorkLog(Date remindMonth) {
		Set<Integer> set = new HashSet<Integer>(31);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c1 = Calendar.getInstance();
		c1.setTime(remindMonth);

		Calendar c2 = Calendar.getInstance();
		c2.setTime(new Date());
		if (c1.get(Calendar.YEAR) < c2.get(Calendar.YEAR)
				|| (c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR) && c1
						.get(Calendar.MONTH) < c2.get(Calendar.MONTH))) {
			return set;
		}

		if (c1.get(Calendar.YEAR) != c2.get(Calendar.YEAR)
				|| c1.get(Calendar.MONTH) != c2.get(Calendar.MONTH)) {
			c1.set(Calendar.DAY_OF_MONTH, 1);
		}else{
			c1.setTime(new Date());
		}
		final String start = sdf.format(c1.getTime());
		c1.set(Calendar.DAY_OF_MONTH,
				c1.getActualMaximum(Calendar.DAY_OF_MONTH));
		final String end = sdf.format(c1.getTime());
		SearchCallback callback = new SearchCallback() {
			@Override
			public QLInfo afterBuildQL(boolean newSearch, QLInfo qlInfo) {
				if (newSearch) {
					qlInfo.andClause("a.remindTime >='" + start + "'");
					qlInfo.andClause("a.remindTime <= '" + end + "'");
				}
				return qlInfo;
			}
		};
		WorkLog w=new WorkLog();
		w.setHandleType(3);
		PageInfo<WorkLog> pageInfo =  genericService.search(w, 0, 0, "", "", false, null, callback, true, false);
		for (WorkLog workLog : pageInfo.getData()) {
			c1.setTime(workLog.getRemindTime());
			set.add(c1.get(Calendar.DAY_OF_MONTH));
		}
		return set;
	}

	@Rest(simpleServiceName="loginUser",type="free")
	public List<WorkLog> getExpiredWithoutHandleWorkLog(){
		Date d=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		QLInfo qlInfo=new QLInfo();
		qlInfo.setDisablePermissionFilter(false);
		String ql="select a from WorkLog a where a.handleType=3 and a.remindTime<'"+sdf.format(d)+"'";
		qlInfo.setQl(ql);
		return genericService.searchByQl(qlInfo).getData();
	}
	
	@Rest(simpleServiceName="loginUser",type="free")
	public Boolean updateHandleType(List<WorkLog> model){
		Collection<String> enabledFields=Arrays.asList("handleType");
		for (WorkLog w : model) {
			genericService.update(w, true, enabledFields, null);
		}
		return true;
	}

}
